﻿
// Condition: 12. Extend previous program to support also subtraction and multiplication of polynomials.


using System;



class SubtractAndMultiply

{
    

    static int[] SubArray(int[] array1, int[] array2, int maxArr)
    {
        int[] finalArray = new int[maxArr + 1];
        // subtract two polynoms
        for (int i = 0; i < finalArray.Length; i++)
        {
            finalArray[i] = array1[i] - array2[i];
        }
        return finalArray;
    }

    static int[] Multiply(int[] array1, int[] array2, int maxArr)
    {

        int position;
        int[] multiArr = new int[(array1.Length+array2.Length)-1];

        for (int i = 0; i < array1.Length; i++)
        {
            for (int j = 0; j < array2.Length; j++)
            {
                position = i + j;
                multiArr[position] += array1[i] * array2[j];
            }
        }
        return multiArr;
    }


    static void Main(string[] args)
    {
        // enter rank of two polinomials max x^2 - mean rank 2
        Console.Write("Enter rank of first polynomial: ");
        int rank1 = int.Parse(Console.ReadLine());

        Console.Write("Enter rank of second polynomial: ");
        int rank2 = int.Parse(Console.ReadLine());

        // define maximum of two ranks 
        int maxRank = rank1;
        if (rank2 > rank1)
        {
            maxRank = rank2;
        }

        // enter coefficients for two polinomials
        Console.WriteLine("Enter coefficients for first polinomial");
        int[] coeffArray1 = new int[maxRank + 1];

        for (int i = 0; i <= rank1; i++)
        {
            Console.Write("for x^{0}: ", i);
            coeffArray1[i] = int.Parse(Console.ReadLine());
        }
        Console.WriteLine();

        Console.WriteLine("Enter coefficients for second polinomial");
        int[] coeffArray2 = new int[maxRank + 1];

        for (int i = 0; i <= rank2; i++)
        {
            Console.Write("for x^{0}: ", i);
            coeffArray2[i] = int.Parse(Console.ReadLine());
        }


        // initiate sum array
        int[] sunArray = Multiply(coeffArray1, coeffArray2, maxRank);

        // print the result
        Console.Write("The array of coefficients: { ");

        for (int i = sunArray.Length - 1; i >= 0; i--)
        {
            Console.Write(sunArray[i] + " ");
        }
        Console.Write(" }");
        Console.WriteLine();

        Console.Write("The result of multipling two polynoms is: ");
        for (int i = sunArray.Length - 1; i >= 0; i--)
        {
            if (sunArray[i] == 0)
            {
                continue;
            }
            else if (i == sunArray.Length - 1)
            {
                Console.Write("({0}x^{1})", sunArray[i], i);
            }
            else
            {
                Console.Write("+({0}x^{1})", sunArray[i], i);
            }

        }
        Console.WriteLine();
    }
}
